// Alessio


// Added comment

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.viper;

import com.example.android.viper.R;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

// Merging

/**
 * TileView: a View-variant designed for handling arrays of "icons" or other
 * drawables.
 * 
 */
public class TileView extends View
{

	/**
	 * Parameters controlling the size of the tiles and their range within view.
	 * Width/Height are in pixels, and Drawables will be scaled to fit to these
	 * dimensions. X/Y Tile Counts are the number of tiles that will be drawn.
	 */

	protected static int mTileSize;

	protected static int mXTileCount = 19;
	protected static int mYTileCount = 19;

	private static int mXOffset;
	private static int mYOffset;
	
	protected static int keyPressed=0;
	protected static float pointX=0;
	protected static float pointY=0;
	
	protected int orientation;	
	
	private Paint paint = new Paint();

	/**
	 * A hash that maps integer handles specified by the subclasser to the
	 * drawable that will be used for that reference
	 */
	private Bitmap[] mTileArray;

	/**
	 * A two-dimensional array of integers in which the number represents the
	 * index of the tile that should be drawn at that locations
	 */
	private int[][] mTileGrid = new int[mXTileCount][mYTileCount];

	private final Paint mPaint = new Paint();

	public TileView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.TileView);

		mTileSize = a.getInt(R.styleable.TileView_tileSize, 17);
		
		clearTiles();		

		a.recycle();
	}

	public TileView(Context context, AttributeSet attrs)
	{
		super(context, attrs);

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.TileView);

		mTileSize = a.getInt(R.styleable.TileView_tileSize, 17);
		
		clearTiles();

		a.recycle();
	}

	/**
	 * Rests the internal array of Bitmaps used for drawing tiles, and sets the
	 * maximum index of tiles to be inserted
	 * 
	 * @param tilecount
	 */

	public void resetTiles(int tilecount)
	{
		mTileArray = new Bitmap[tilecount];
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		//mXTileCount = (int) Math.floor(w / mTileSize);
		//mYTileCount = (int) Math.floor(h / mTileSize);
		
		mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
		mYOffset = ((h - (mTileSize * mYTileCount)) / 2); // = 0;

		//mTileGrid = new int[mXTileCount][mYTileCount];
		//clearTiles();
	}

	/**
	 * Function to set the specified Drawable as the tile for a particular
	 * integer key.
	 * 
	 * @param key
	 * @param tile
	 */
	public void loadTile(int key, Drawable tile)
	{
		Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		tile.setBounds(0, 0, mTileSize, mTileSize);
		tile.draw(canvas);

		mTileArray[key] = bitmap;
	}
	
	/**
	 * Resets all tiles to 0 (empty)
	 * 
	 */
	public void setColorTiles(int h, int st, int en)
	{
		for (int index=st; index<=en; index++)
		{
			Bitmap bmp=mTileArray[index];
			if (bmp!=null)
			{
				for (int x=0; x<bmp.getWidth(); x++)
				{
					for (int y=0; y<bmp.getHeight(); y++)
					{
						float hsv[] = new float[3];
						int col=bmp.getPixel(x, y);
						
						Color.RGBToHSV(Color.red(col), Color.green(col), Color.blue(col), hsv);
						hsv[0]=h;
						//Log.d("ViperView",""+Integer.toHexString(col));
						if (col!=Color.BLACK)
						{
							bmp.setPixel(x, y, Color.HSVToColor(hsv));
						}
					}				
				}
			}
		}
	}	

	/**
	 * Resets all tiles to 0 (empty)
	 * 
	 */
	public void clearTiles()
	{
		for (int x = 0; x < mXTileCount; x++)
		{
			for (int y = 0; y < mYTileCount; y++)
			{
				setTile(0, x, y);
			}
		}
	}

	/**
	 * Used to indicate that a particular tile (set with loadTile and referenced
	 * by an integer) should be drawn at the given x/y coordinates during the
	 * next invalidate/draw cycle.
	 * 
	 * @param tileindex
	 * @param x
	 * @param y
	 */
	public void setTile(int tileindex, int x, int y)
	{
		mTileGrid[x][y] = tileindex;
	}
	
	/**
	 * Used to indicate that a particular tile (set with loadTile and referenced
	 * by an integer) should be drawn at the given x/y coordinates during the
	 * next invalidate/draw cycle.
	 * 
	 * @param tileindex
	 * @param x
	 * @param y
	 */
	int getTile(int x, int y)
	{
		return mTileGrid[x][y];
	}	
	
	void drawRect(Canvas canvas, int x1, int y1, int x2, int y2, int thick, int col)
	{
        paint.setColor(Color.rgb(127, 0, 0));
        //paint.setStrokeWidth(4);
        Rect rect = new Rect(x1, y1, x2, y2);
        RectF rectF = new RectF(rect);
        canvas.drawRoundRect( rectF, 7, 7, paint);
        
        paint.setColor(col);
        //paint.setStrokeWidth(4);
        rect = new Rect(x1 + thick, y1 + thick, x2 - thick, y2 - thick);
        rectF = new RectF(rect);
        canvas.drawRoundRect( rectF, 7, 7, paint);

	}
	
	@Override
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		for (int x = 0; x < mXTileCount; x += 1)
		{
			for (int y = 0; y < mYTileCount; y += 1)
			{
				int tile=mTileGrid[x][y];
				
//				if (orientation==Configuration.ORIENTATION_PORTRAIT)
//				{
//					tile = mTileGrid[x][y];
//				}
//				if (orientation==Configuration.ORIENTATION_LANDSCAPE)
//				{
//					tile = mTileGrid[y][x];
//				}
				
				if (tile > 0)
				{
					canvas.drawBitmap(mTileArray[tile], mXOffset + x
							* mTileSize, mYOffset + y * mTileSize, mPaint);
				}
			}
		}
		//Log.d("ViperView", "Secon "+ System.currentTimeMillis());
		//drawRect(canvas, 5, 327, 315, 503);
		if (orientation==Configuration.ORIENTATION_LANDSCAPE)
		{
			drawRect(canvas, 110, 327, 210, 387, 4, (keyPressed==KeyEvent.KEYCODE_DPAD_UP)?Color.GRAY:Color.BLACK);
			drawRect(canvas, 110, 443, 210, 503, 4, (keyPressed==KeyEvent.KEYCODE_DPAD_DOWN)?Color.GRAY:Color.BLACK);
			drawRect(canvas, 5, 385, 105, 445, 4, (keyPressed==KeyEvent.KEYCODE_DPAD_LEFT)?Color.GRAY:Color.BLACK);
			drawRect(canvas, 215, 385, 315, 445, 4, (keyPressed==KeyEvent.KEYCODE_DPAD_RIGHT)?Color.GRAY:Color.BLACK);
		}
		
		//paint.setColor(Color.GREEN);
		//canvas.drawCircle(pointX, pointY, 5, paint);
	}

}
